<?php
namespace app\user\controller\admin;

use app\common\controller\Back;

class Config extends Back
{
    protected function initialize()
    {
        parent::initialize();
    }
    
    public function index()
    {
        $form = dcFormCreate([
            'name'         => 'adminUserIndex',
            'action'       => dcUrl('user/admin.config/update'),
            'items'        => $this->formItems(config('user.')),
            'reset'        => '重置',
            'class_button' => 'text-left',
        ]);

        $this->assign('form',$form);

        $this->assign($this->site);

        return $this->fetch('admin.config/index');
    }

    public function update()
    {
        $post = dcArrayFilter(input('post.'), array_keys($this->formItems()));
        if(!$post){
            $this->error('参数错误');
        }

        $this->configSave('./config/user.php', $post, 'user/admin.config/index');
    }
    
    private function formItems($data=[])
    {
        return [
            //短信验证
            'code' => [
                'type'        => 'radio',
                'value'       => $data['code'],
                'option'      => model('common/Option','loglic')->switch(),
                'title'       => '注册需要短信验证',
                //'tips'        => '开启后需要正确配置短信发送平台，否则不能正常注册。',
            ],
            //加密密钥
            'secret' => [
                'type'        => 'text',
                'value'       => dcEmpty($data['secret'],uniqid()),
                'title'       => 'COOKIE加密密钥',
                'required'    => false,
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
            //TOKEN有效期（天）
            'expire' => [
                'type'        => 'text',
                'value'       => intval($data['expire']),
                'title'       => 'TOKEN有效期（天）',
                'required'    => true,
                'class_row'   => 'mb-2 border-bottom pb-3',
                'class_label' => 'form-label',
            ],
            //初始角色组
            'roles' => [
                'type'        => 'select',
                'value'       => $data['roles'],
                'option'      => model('common/Option','loglic')->roleForm(),
                'multiple'    => true,
                'size'        => 3,
                'required'    => true,
                'title'       => '初始角色组',
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
            //初始独立节点
            'caps' => [
                'type'        => 'textarea',
                'value'       => implode(chr(13),$data['caps']),
                'rows'        => 3,
                'required'    => false,
                'title'       => '初始权限节点',
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
        ];
    }
}